package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

@SuppressWarnings("serial")
public class TodayTasksListingPanel extends JPanel {
	private String[] columnNames;
	private JScrollPane scrollPane;
	private JTable table;
	private static String[][] todayTaskTable;
	private DefaultTableModel model;
	CustomTableCellRenderer cellRenderer;

	public TodayTasksListingPanel() {
		setEnvironment();

		columnNames = new String[] { "Start", "End", "Task" };

		model = new DefaultTableModel();
		for (String column : columnNames) {
			model.addColumn(column);
		}

		table = new JTable(model) {
			public boolean isCellEditable(int row, int col) {
				return false;
			};

			{
				setOpaque(false);
				setDefaultRenderer(Object.class,
						new DefaultTableCellRenderer() {
							{
								setOpaque(false);
							}
						});
			}
		};
		ImageIcon icon = new ImageIcon(ImagesDirectory.getTodayTasksBG());
		final Image backgroundImage = icon.getImage();
		final ImageObserver imageObserver = icon.getImageObserver();

		scrollPane = new JScrollPane(table) {
			{
				setOpaque(false);
				getViewport().setOpaque(false);

			}

			@Override
			protected void paintComponent(Graphics g) {
				// Draw the background image.
				g.drawImage(backgroundImage, 0, 0, Dimensions._windowWidth,
						Dimensions._windowHeight, imageObserver);
				super.paintComponent(g);
			}
		};

		setScrollpaneProperties();
		setTableProperties();

		add(scrollPane, BorderLayout.CENTER);

		table.repaint();
	}

	private void setTableProperties() {
		table.setFillsViewportHeight(true);
		table.setShowGrid(false);
		table.getTableHeader().setReorderingAllowed(false);
		table.setRowHeight(20);
		initTableColumns();
		table.getTableHeader().setOpaque(false);
	}

	private void setScrollpaneProperties() {
		scrollPane.getVerticalScrollBar().setOpaque(false);
		scrollPane.getVerticalScrollBar().setUI(new CustomizedScrollUI());
		scrollPane.setColumnHeader(new JViewport());
		scrollPane.getColumnHeader().setOpaque(false);
	}

	private void setEnvironment() {
		setLayout(new BorderLayout());
		setOpaque(false);
	}

	private void initTableColumns() {
		CustomTableCellRenderer cellRenderer = new CustomTableCellRenderer();
		CustomHeaderRenderer headerRenderer = new CustomHeaderRenderer();
		TableColumn tableColumn = table.getColumnModel().getColumn(0);
		tableColumn.setMinWidth(50);
		tableColumn.setMaxWidth(50);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);
		tableColumn = table.getColumnModel().getColumn(1);
		tableColumn.setMinWidth(50);
		tableColumn.setMaxWidth(50);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);
		tableColumn = table.getColumnModel().getColumn(2);
		tableColumn.setHeaderRenderer(headerRenderer);
		tableColumn.setCellRenderer(cellRenderer);

	}

	private void loadTable(String[][] taskTable) {
		clearTable();
		for (int i = 0; i < todayTaskTable.length; i++) {
			model.addRow(todayTaskTable[i]);
		}
		table.validate();
		table.repaint();
	}

	private void clearTable() {
		int rows = model.getRowCount();
		for (int i = 0; i < rows; i++) {
			model.removeRow(0);
		}
	}

	protected void updateTodayTable(String[][] todayTasks) {
		todayTaskTable = todayTasks;
		loadTable(todayTaskTable);
	}

	public class CustomTableCellRenderer extends JLabel implements
			TableCellRenderer {

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 13));
			setText((String) value);
			setHorizontalAlignment(SwingConstants.CENTER);
			setForeground(new Color(51, 51, 51));
			return this;
		}
	}

	public class CustomHeaderRenderer extends JLabel implements
			TableCellRenderer {

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 14));
			setText((String) value);
			setHorizontalAlignment(SwingConstants.CENTER);
			setForeground(new Color(0, 0, 0));
			return this;
		}
	}
}
